﻿package mortal.game.view.guildNew.manor
{
    import Message.Public.*;
    import extend.language.*;
    import mortal.common.*;
    import mortal.game.cache.*;
    import mortal.game.cache.guild.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.utils.*;
    import mortal.game.view.copy.QuestionCopy.data.*;
    import mortal.game.view.guild.otherpanel.*;
    import mortal.game.view.guildNew.*;
    import mortal.game.view.guildNew.data.*;
    import mortal.game.view.guildNew.otherGuild.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class GuildNewManorProcessor extends Object
    {
        private var _view:IView;
        private var _actionDate:Date = null;
        private var _state:int = 1;

        public function GuildNewManorProcessor()
        {
            this.initServer();
            return;
        }// end function

        public function initView(param1:IView) : void
        {
            this._view = param1;
            return;
        }// end function

        public function initServer() : void
        {
            EventUtil.addSelfEvent(EventName.GUILD_SEARCH, this.searchGuild, true);
            EventUtil.addNetEvent(ServerCommand.GUILD_LIST_UPDATE, this.guildListUpdate, true);
            EventUtil.addSelfEvent(EventName.Guild_enterManor, this.enterManorRequest, true);
            EventUtil.addSelfEvent(GuildNewLocalEvent.GuildNewOpenGardenP, this.onOpenGarden, true);
            EventUtil.addNetEvent(ServerCommand.GUILD_INFO_UPDATE, this.levelUpCheck, true);
            Dispatcher.addEventListener(EventName.ActiveNotic, this.activesNoticHandler);
            Dispatcher.addEventListener(EventName.ActiveStart, this.activesStarHandler);
            Dispatcher.addEventListener(EventName.ActiveEnd, this.activesEndHandler);
            LevelCheckUtil.addLevelUp(this.levelUpCheck);
            EventUtil.addNetEvent(ServerCommand.GUILD_INFO_UPDATE, this.onGuildInfoLoad, true);
            return;
        }// end function

        public function onViewShow() : void
        {
            this.addEvs(true);
            return;
        }// end function

        public function onViewHide() : void
        {
            this.addEvs(false);
            return;
        }// end function

        private function addEvs(param1:Boolean) : void
        {
            return;
        }// end function

        private function searchGuild(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            GameProxy.guild.searchGuilds(_loc_2.camp, _loc_2.guildName, _loc_2.startIdx, _loc_2.isfull);
            return;
        }// end function

        private function guildListUpdate(param1:Object = null) : void
        {
            var _loc_2:GuildNewOtherPanel = null;
            if (this._view && this._view.isHide == false)
            {
                _loc_2 = (this._view as GuildNewModule).curPanel as GuildNewOtherPanel;
                if (_loc_2)
                {
                    _loc_2.update();
                }
                if (GuildNewUtil.hadIndexFruits() > -1)
                {
                    (this._view as GuildNewModule).updateFruitsLabel(true);
                }
                else
                {
                    (this._view as GuildNewModule).updateFruitsLabel(false);
                }
            }
            if (GuildListPanel.hasInstance && GuildListPanel.instance.isHide == false)
            {
                GuildListPanel.instance.update();
            }
            this.showFruiteIcon();
            return;
        }// end function

        private function enterManorRequest(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            var _loc_3:* = Cache.instance;
            if (event.data == 0)
            {
                if (_loc_3.guild.selfGuildInfo.baseInfo == null)
                {
                    MsgManager.showRollTipsMsg("" + Language.getString(71499));
                    return;
                }
                _loc_2 = _loc_3.guild.selfGuildInfo.baseInfo.guildId;
            }
            CopyUtil.copyCheck(this.enterAfterCheck, _loc_2);
            return;
        }// end function

        private function enterAfterCheck(param1:int) : void
        {
            AIManager.cancelAll();
            GameProxy.copy.enterGuildManor(param1);
            return;
        }// end function

        private function activesNoticHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SClientActive;
            if (_loc_2.type != EActiveType._EActiveTypeGuildGarden)
            {
                return;
            }
            var _loc_3:* = _loc_2.startDt.time - ClockManager.instance.nowDate.time;
            if (_loc_3 <= 0)
            {
                return;
            }
            this._actionDate = _loc_2.startDt;
            this._state = 1;
            this.showFruiteIcon();
            return;
        }// end function

        private function activesStarHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SClientActive;
            if (_loc_2.type != EActiveType._EActiveTypeGuildGarden)
            {
                return;
            }
            var _loc_3:* = Math.ceil((_loc_2.endDt.time - ClockManager.instance.nowDate.time) / 1000);
            if (_loc_3 <= 0)
            {
                return;
            }
            this._actionDate = _loc_2.endDt;
            this._state = 2;
            this.showFruiteIcon();
            return;
        }// end function

        private function showFruiteIcon() : void
        {
            if (this._actionDate && this.canShowGuildIcon())
            {
                GuildNewFruitsIcon.instance.show();
                GuildNewFruitsIcon.instance.setShowState(this._actionDate, this._state);
            }
            return;
        }// end function

        private function onGuildInfoLoad(param1:Object) : void
        {
            this.showFruiteIcon();
            return;
        }// end function

        private function activesEndHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SClientActive;
            if (_loc_2.type != EActiveType._EActiveTypeGuildGarden)
            {
                return;
            }
            this._actionDate = null;
            this._state = 1;
            GuildNewFruitsIcon.instance.stopEffect();
            GuildNewFruitsIcon.instance.hide();
            return;
        }// end function

        private function canShowGuildIcon() : Boolean
        {
            var _loc_1:SelfGuildInfo = null;
            if (LevelCheckUtil.curLevel >= 45)
            {
                _loc_1 = Cache.instance.guild.selfGuildInfo;
                if (_loc_1 && _loc_1.baseInfo && _loc_1.baseInfo.level >= 3)
                {
                    return true;
                }
            }
            return false;
        }// end function

        private function levelUpCheck(param1:Object) : void
        {
            this.showFruiteIcon();
            return;
        }// end function

        private function onOpenGarden(event:DataEvent) : void
        {
            var _loc_2:* = Cache.instance.guild.selfGuildInfo;
            if (_loc_2 == null || _loc_2.baseInfo == null || _loc_2.baseInfo.guildId == 0)
            {
                MsgManager.showRollTipsMsg("" + Language.getString(71500));
                return;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildOpen, "m4"));
            return;
        }// end function

    }
}
